package com.nnnu.orange.controller.admin;


import com.nnnu.orange.constant.MessageConstants;
import com.nnnu.orange.constant.StatusCodeConstants;
import com.nnnu.orange.pojo.dto.BookDto;
import com.nnnu.orange.pojo.dto.BooksQueryDto;
import com.nnnu.orange.pojo.entity.Books;
import com.nnnu.orange.pojo.vo.BooksInfoVo;
import com.nnnu.orange.result.PageResult;
import com.nnnu.orange.result.Result;
import com.nnnu.orange.service.IBooksService;
import com.nnnu.orange.utils.ExcelParser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 存储每本图书的详细信息 前端控制器
 * </p>
 *
 * @author zk
 * @since 2024-11-07
 */
@RestController("adminBooksController")
@RequestMapping("/admin/books")
@RequiredArgsConstructor
@Api(tags = "管理端图书相关接口")
@Slf4j
public class BooksController {

    private final IBooksService iBooksService;

    /**
     * 分页查询图书列表
     *
     * @param queryDto dto
     * @return 返回前端数据 分页图书列表
     */
    @GetMapping
    @ApiOperation("分页查询图书列表")
    public Result<PageResult<BooksInfoVo>> listBooks(BooksQueryDto queryDto) {
        log.info("分页查询图书列表:{}", queryDto);
        return Result.success(iBooksService.listBooks(queryDto), MessageConstants.SELECT_OK);
    }


    /**
     * 获取单本图书的详细信息
     *
     * @param id 图书id
     * @return 返回前端图书实体
     */
    @ApiOperation("获取单本图书的详细信息")
    @GetMapping("/{id}")
    public Result<Books> getOne(@PathVariable Integer id) {
        log.info("获取单本图书的详细信息:{}", id);
        return Result.success(iBooksService.getById(id), MessageConstants.SELECT_OK);
    }


    /**
     * 新增图书
     *
     * @param bookDto dto
     * @return 返回前端提示信息
     */
    @PostMapping
    @ApiOperation("新增图书")
    public Result<String> save(@RequestBody @Validated BookDto bookDto) {
        log.info("新增图书:{}", bookDto);
        return Result.success(iBooksService.save(bookDto) ? MessageConstants.ADD_OK : MessageConstants.NULL);
    }


    /**
     * 修改图书信息
     *
     * @param bookDto 图书dto
     * @return 返回前端提示信息
     */
    @PutMapping
    @ApiOperation("修改图书信息")
    public Result<String> update(@RequestBody @Validated BookDto bookDto) {
        log.info("修改图书信息:{}", bookDto);
        iBooksService.updateById(bookDto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }


    /**
     * 批量删除图书
     *
     * @param ids 图书id列表
     * @return 返回前端提示信息
     */
    @DeleteMapping
    @ApiOperation("批量删除图书")
    public Result<String> deleteBatch(@RequestParam List<Integer> ids) {
        log.info("批量删除图书:{}", ids);
        Boolean b = iBooksService.deleteBatch(ids);
        return Result.success(b ? StatusCodeConstants.SUCCESS : StatusCodeConstants.INTERNAL_SERVER_ERROR, b ? MessageConstants.OPERATION_SUCCESSFUL : MessageConstants.DELETE_FAILED + "书籍可能存在上架中");
    }

    @PostMapping("/upload")
    @ApiOperation("上传 Excel 文件并批量导入图书数据")
    public Result<String> uploadExcel(@RequestParam("file") MultipartFile file) {
        try {
            // 将上传的文件转换为 InputStream
            InputStream inputStream = file.getInputStream();

            // 解析 Excel 文件
            ExcelParser excelParser = new ExcelParser();
            List<Books> booksList = excelParser.parseExcel(inputStream);

            // 将解析后的数据批量保存到数据库
            iBooksService.batchInsert(booksList);
            return Result.success("数据导入成功");
        } catch (Exception e) {
            log.error("Excel文件上传失败", e);
            return Result.error(StatusCodeConstants.INTERNAL_SERVER_ERROR, "数据导入失败：" + e.getMessage());
        }
    }
}
